<?php
define("type_comment", "comment");
define("type_rate", "rate");

function get_comment_and_rate_by_moduleId($moduleId, $module, $userName) {
	$commentObjModel = M("module_comment_object");
	$comment = $commentObjModel->where("moduleId=$moduleId AND module='$module'")->find();
	if ($comment) {
		$comment["alreadyComment"] = 0;
		$comment["alreadyRate"] = 0;
		$objId = $comment["id"];
		if ($comment["commentCount"] > 0) {
			$commentModel = M("module_comment");
			$arr = $commentModel->where("objectId=$objId AND type='" . type_comment ."'")->select();
			$comment["comments"] = $arr;
		}
		$CommentModel = M("module_comment");
		$where["objectId"] = $objId;
		$where["fromUserName"] = $userName;
		$arr = $CommentModel->where($where)->select();
		if ($arr) {
			foreach ($arr as $commentUser) {
				if ($commentUser["type"] == type_comment) {
					$comment["alreadyComment"] = 1;
				}
				if ($commentUser["type"] == type_rate) {
					$comment["alreadyRate"] = 1;
				}
			}
		}
	}
	return $comment;
}


function get_comment_and_rate_obj_by_moduleId($moduleId, $module) {
	$commentObjModel = M("module_comment_object");
	$comment = $commentObjModel->where("moduleId=$moduleId AND module='$module'")->find();
	return $comment;
}

function comment_by_moduleId($moduleId, $module, $comment, $userName, $fullName = NULL) {
	$commentObjModel = M("module_comment_object");
	$obj = $commentObjModel->where("module='$module' AND moduleId=$moduleId")->find();
	if ($obj) {
		$objId = $obj["id"];
		$commentObjModel->where("id=$objId")->setInc("commentCount");
	} else {
		$dataObj["module"] = $module;
		$dataObj["moduleId"]  = $moduleId;
		$dataObj["commentCount"] = 1;
		$objId = $commentObjModel->add($dataObj);
	}
	
	//TODO:添加detail,set_point_by_point_type(module_handle_comment, $userName,false,detail) ;
	//添加积分
	set_point_by_point_type(module_handle_comment, $userName,false,$comment) ;
	
	$CommentModel = M("module_comment");
	$data["objectId"] = $objId;
	$data["comment"] = $comment;
	$data["fromUserName"] = $userName;
	$data["fullName"] = $fullName;
	$data["updateTime"] = date("Y-m-d H:i:s");
	$data["type"] = type_comment;
	$result = $CommentModel->add($data);

	if ($fullName != NULL) {
		update_UserInfo_by_message($userName, $fullName, user_full_name);
	}
}


function rate_by_moduleId($moduleId, $module, $rate, $userName) {
	$commentObjModel = M("module_comment_object");
	$obj = $commentObjModel->where("module='$module' AND moduleId=$moduleId")->find();
	if ($obj) {
		$objId = $obj["id"];
		$ratePrevious = $obj["rateCount"];
		$dataObj["rateCount"] = ceil(($ratePrevious + $rate) / 2);
		$commentObjModel->where("id=$objId")->save($dataObj);
	} else {
		$dataObj["module"] = $module;
		$dataObj["moduleId"]  = $moduleId;
		$dataObj["rateCount"] = $rate;
		$objId = $commentObjModel->add($dataObj);
	}

	//TODO:添加detail,set_point_by_point_type(module_handle_comment, $userName,false,detail) ;
	//添加积分
	set_point_by_point_type(module_handle_rate, $userName,false) ;
	
	$CommentModel = M("module_comment");
	$data["objectId"] = $objId;
	$data["rate"] = $rate;
	$data["fromUserName"] = $userName;
	$data["updateTime"] = date("Y-m-d H:i:s");
	$data["type"] = type_rate;
	$result = $CommentModel->add($data);
}